﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Net.Mime.MediaTypeNames;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://CPFUI/Style/Css/Normal.css")]//用于设计的时候加载样式
    public class IMChatBoxItem : Control
    {
        public Ellipse HeadPicture;

        public StackPanel Content;
        public StackPanel PnlUser;

        public Visibility ShowUserInfo = Visibility.Visible;
        public MessagesInfo MessagesInfo
        {
            get { return GetValue<MessagesInfo>(); }
            set { SetValue(value); }
        }
        public IMChatBoxItem(MessagesInfo messages)
        {
            MessagesInfo = messages;
        }
        //模板定义
        protected override void InitializeComponent()
        {
            IsAntiAlias = true;
            AddMessage();
        }
        public void AddMessage()
        {
            if (MessagesInfo == null)
            {
                return;
            }
            if (MessagesInfo.SendReceiveType == EChatType.Send)
            {
                Content = new StackPanel
                {
                    Orientation= Orientation.Vertical,
                    MarginTop = ShowUserInfo == Visibility.Visible ? 25 : 6,
                    MarginRight = 40,
                    CornerRadius = "8",
                    MinHeight = 20,
                    Background = Color.FromArgb(20,0,0,0),
                    MinWidth = 20,
                    MarginBottom = 5,
                };
                PnlUser = new StackPanel
                {
                    Visibility = ShowUserInfo,
                    Orientation = Orientation.Horizontal,
                    MarginTop = 6,
                    MarginRight = 40,
                    Children =
                    {
                        new TextBox{
                            Background=null,
                            BorderFill=null,
                            IsReadOnly = true,
                            Text =string.Format("{0}  {1}",DateTime.Now.ToString("HH:mm:ss"),MessagesInfo.User.NickName),
                            Classes={ "chatBoxHeadTextBox" }
                        }
                    }
                }; 
                HeadPicture = new Ellipse()
                {
                    MarginRight = 5,
                    MarginTop = 5,
                    IsAntiAlias = true,
                    Width = 30,
                    Height = 30,
                    StrokeFill = "#ff0",
                    Fill = string.Format("url({0}) Clamp Fill 0,0,0,0", MessagesInfo.User.HeadImage),
                };
            }
            else
            {
                Content = new StackPanel
                {
                    Orientation = Orientation.Vertical,
                    MarginTop = ShowUserInfo == Visibility.Visible ? 25 : 6,
                    MarginLeft = 40,
                    CornerRadius = "8",
                    MinHeight = 20,
                    Background = Color.FromArgb(20, 0, 0, 0),
                    MinWidth = 20,
                    MarginBottom = 5,
                };
                PnlUser = new StackPanel
                {
                    Visibility = ShowUserInfo,
                    Orientation = Orientation.Horizontal,
                    MarginTop = 6,
                    MarginLeft = 40,
                    Children =
                    {
                        new TextBox{
                            Background=null,
                            BorderFill=null,
                            IsReadOnly = true,
                            Text =string.Format("{0}  {1}",MessagesInfo.User.NickName,DateTime.Now.ToString("HH:mm:ss")),
                            Classes={ "chatBoxHeadTextBox" }
                        }
                    }
                };
                HeadPicture = new Ellipse()
                {
                    MarginLeft = 5,
                    MarginTop = 5,
                    IsAntiAlias = true,
                    Width = 30,
                    Height = 30,
                    StrokeFill = "#ff0",
                    Fill = string.Format("url({0}) Clamp Fill 0,0,0,0",MessagesInfo.User.HeadImage),
                };
            }
            Children.Add(HeadPicture);
            Children.Add(PnlUser);
            Children.Add(Content);
            for (int i = 0; i < MessagesInfo.Data.Count; i++)
            {
                switch (MessagesInfo.Data[i].MsgType)
                {
                    case EMsgType.Text:
                        Addmsg(MessagesInfo.Data[i].Information.ToString());
                        break;
                    case EMsgType.Image:
                        AddPictrue(MessagesInfo.Data[i].Information.ToString());
                        break;
                    case EMsgType.File:
                        AddFile(MessagesInfo.Data[i].Information.ToString());
                        break;
                    case EMsgType.Voice:
                        break;
                    case EMsgType.Video:
                        break;
                    case EMsgType.RedEnvelope:
                        break;
                    case EMsgType.Custom:
                        AddCustom((UIElement)MessagesInfo.Data[i].Information);
                        break;
                }
            }
        }
        private void Addmsg(string msg)
        {
            TextBox text = new TextBox()
            {
                Background = null,
                BorderFill = null,
                MarginLeft = 5,
                MarginTop = 5,
                MarginRight = 5,
                MarginBottom = 5,
                IsReadOnly= true,
                Text = msg,
                Classes = { "chatBoxTextBox" }
            };
            Content.Children.Add(text);
            //SetAutoSize();
        }
        private void AddPictrue(string  url)
        {
            Bitmap bitmap=new Bitmap(url);
            int w=bitmap.Width;
            int h=bitmap.Height;
            if (w > h)
            {
                if (w > 200)
                {
                    h = (int)(h * 1.0f * 200 / w);
                    w = 200;
                }
            }
            else
            {
                w = (int)(w * 1.0f * 200 / h);
                h = 200;
            }
            Content.Children.Add(
                new Panel
                {
                    MarginLeft = 0,
                    MarginTop = 0,
                    MarginRight = 0,
                    MarginBottom = 0,
                    Background= string.Format("url({0}) Clamp Fill 0,0,0,0", url),

                    Height = h,
                    Width = w,
                    ClipToBounds = true,
                    CornerRadius="8",

                });
            
            //SetAutoSize();
        }
        private void AddFile(string fileName)
        {
            Content.Children.Add(
                new Panel
                {
                    Background=Color.White,
                    Width=300,
                    Height=100,
                    BorderFill=Color.Silver,
                    BorderStroke=new Stroke(1),
                    CornerRadius="6",
                    MarginLeft = 5,
                    MarginTop = 5,
                    MarginRight = 5,
                    MarginBottom = 5,
                    Children =
                    {
                        new Panel
                        {
                            MarginLeft=0,
                            MarginRight=0,
                            MarginTop=0,
                            MarginBottom=30,
                            Children =
                            {
                                new Panel
                                {
                                    MarginLeft=10,
                                    MarginBottom=10,
                                    MarginTop=10,
                                    BorderFill=Color.Silver,
                                    BorderStroke="1",
                                    Width=40,
                                    CornerRadius="2",
                                    Children =
                                    {
                                        new Picture()
                                        {
                                            Stretch = Stretch.Uniform,
                                            Source = "res://CPFUI/Resources/file.png",
                                        }
                                    }
                                },

                                new TextBlock
                                {
                                    Foreground=Color.Gray,
                                    MarginRight=10,
                                    Width=70,
                                    Text="(1024MB)"
                                },
                                new TextBlock
                                {
                                    MarginLeft=60,
                                    MarginRight=90,
                                    MarginBottom=10,
                                    MarginTop=10,
                                    VerticalAlignment=VerticalAlignment.Center,
                                    Text=fileName,
                                    UseEllipsisToolTip=true
                                }
                            }
                        },
                        new Panel
                        {
                            MarginBottom=-1,
                            Height=30,
                            MarginLeft=-1,
                            MarginRight=-1,
                            BorderFill=Color.Silver,
                            CornerRadius="0,0,6,6",
                            BorderStroke="1",
                            BorderThickness=new Thickness(0,1,0,0),
                            Children =
                            {
                                new TextBlock
                                {
                                    Cursor=Cursors.Hand,
                                    Text="来自群文件",
                                    Foreground=Color.Gray,
                                    MarginLeft=10,
                                },
                                new StackPanel
                                {
                                    Orientation=Orientation.Horizontal,
                                    MarginRight=10,
                                    Children =
                                    {
                                        new TextBlock
                                        {
                                            Cursor=Cursors.Hand,
                                            Text="下载",
                                            Foreground=Color.DodgerBlue,
                                            MarginLeft=10,
                                            
                                        },
                                        new TextBlock
                                        {
                                            Cursor=Cursors.Hand,
                                            Text="另存为",
                                            Foreground=Color.DodgerBlue,
                                            MarginLeft=10,
                                        },
                                        new TextBlock
                                        {
                                            Cursor=Cursors.Hand,
                                            Text="转发",
                                            Foreground=Color.DodgerBlue,
                                            MarginLeft=10,
                                        },
                                        new Panel{
                                            Cursor=Cursors.Hand,
                                            MarginLeft=10,
                                            Children={ 
                                                new Line
                                                {
                                                    StrokeStyle=new Stroke(1.5f),
                                                    StrokeFill=Color.DodgerBlue,
                                                    StartPoint=new Point(0,0),
                                                    EndPoint=new Point(13,0),
                                                },

                                                new Line
                                                {
                                                    StrokeStyle=new Stroke(1.5f),
                                                    StrokeFill=Color.DodgerBlue,
                                                    StartPoint=new Point(0,10),
                                                    EndPoint=new Point(13,10),
                                                },
                                                new Line
                                                {
                                                    StrokeStyle=new Stroke(1.5f),
                                                    StrokeFill=Color.DodgerBlue,
                                                    StartPoint=new Point(0,5),
                                                    EndPoint=new Point(13,5),
                                                }
                                            }
                                        },
                                    }
                                }
                            }
                        },
                    }
                });
        }
        private void AddCustom(UIElement msg)
        {
            msg.MarginLeft = 5;
            msg.MarginTop = 5;
            msg.MarginRight = 5;
            msg.MarginBottom = 5;
            Content.Children.Add(
                msg);
        }
    }
}
